package com.example.psysystemhomework.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.example.psysystemhomework.R;
import com.example.psysystemhomework.bean.AnswerBean;
import com.example.psysystemhomework.bean.CountBeanStatic;
import com.example.psysystemhomework.bean.DoctorBean;
import com.example.psysystemhomework.bean.RBean;
import com.example.psysystemhomework.bean.StoryBean;
import com.example.psysystemhomework.util.NetUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import lombok.Data;
import lombok.SneakyThrows;

@Data
public class ReplyAdapter extends BaseAdapter {
    private static final String TAG = "storyBean 获取过来的";
    JSONObject json;
    Context context;
    private List<AnswerBean> answerBeans;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            RBean rBean = null;
            String jsonObject = msg.obj.toString();
            Gson gson = new Gson();
            if (jsonObject != null) {
                rBean = gson.fromJson(jsonObject, RBean.class);
            }
            Log.i("handle传递过来的", "handleMessage: " + rBean.getData());
            try {
                if (rBean.getData() != null) {
                    JSONObject object = new JSONObject(rBean.getData().toString().replace("\r\n", ""));
                    StoryBean storyBean = gson.fromJson(object.toString(), StoryBean.class);
                    showALert(context, storyBean.getType() + "", storyBean.getContent().trim(), "取消", null);
                }
                else {
                    showALert(context, "提示" + "", "该故事已被删除", "取消", null);

                }

            } catch (JSONException e) {
                e.printStackTrace();
            }

            super.handleMessage(msg);
        }
    };

    public void setJson(JSONObject json) {
        this.json = json;
        Log.i("给适配器传递过来json", "HomeAdapter: " + json.toString());
        String data = null;
        String o = null;
        try {
            data = json.getString("data");
            JSONObject jsonObject = new JSONObject(data);
            o = jsonObject.get("list").toString();
        } catch (JSONException e) {
            e.printStackTrace();
        }

        Gson gson = new Gson();
        answerBeans = gson.fromJson(o, new TypeToken<List<AnswerBean>>() {
        }.getType());
        Log.i(TAG, "setJson: " + answerBeans);
        CountBeanStatic.reply_count = answerBeans.size();
    }

    public ReplyAdapter(JSONObject json, Context context) {
        this.json = json;
        this.context = context;
        if (json.isNull("data")) {
            answerBeans = new ArrayList<>();
        }


    }

    @SneakyThrows
    @Override
    public int getCount() {
        return answerBeans.size();
    }

    @Override
    public Object getItem(int i) {
        return answerBeans.get(i);
    }

    @SneakyThrows
    @Override
    public long getItemId(int i) {
        return i;
    }

    @SneakyThrows
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        view = View.inflate(context, R.layout.item_reply_show, null);

        ViewHolder viewHolder = new ViewHolder(view);
        DoctorBean doctorBean = answerBeans.get(i).getDoctorBean();
        Glide.with(context).load(doctorBean.getIconBean().getUrl())
                .into(viewHolder.mImageTouxiang);
        viewHolder.userName.setText(doctorBean.getName());
//        viewHolder.mContent.setText(answerBeans.get(i).getContent() + "");
        viewHolder.grade.setText(doctorBean.getDoctorTypeBean().getName() + "");
        viewHolder.mTitleName.setText("专业指导");

        String replace = answerBeans.get(i).getContent().trim().replace("\r\n", "");
        if (replace.length() > 42) {
            viewHolder.mContent.setText(replace.substring(0, 42) + "...");
        } else {
            viewHolder.mContent.setText(replace);
        }


        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                AnswerBean item = answerBeans.get(i);
                builder.setTitle(item.getDoctorBean().getName() + "医生：情感指导--" + item.getDoctorBean().getWorkTimeYear() + "年经验");
                builder.setMessage(item.getContent());
                builder.setPositiveButton("查看该回复对应的情感内容", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                        new Thread() {
                            @Override
                            public void run() {
                                NetUtil netUtil = new NetUtil(context);
                                String story_storyId = netUtil.getStory_StoryId(item.getStoryId());
                                Message message = new Message();
                                message.obj = story_storyId;
                                handler.sendMessage(message);
                                super.run();
                            }
                        }.start();


                    }
                });
                builder.show();
            }
        });
        return view;
    }


    public static class ViewHolder {
        ImageView mImageTouxiang;
        TextView mTitleName;
        TextView userName;
        TextView mContent;
        TextView grade;
        ImageView mLogo;

        public ViewHolder(View itemView) {
            this.mImageTouxiang = itemView.findViewById(R.id.image_touxiang);
            this.mTitleName = itemView.findViewById(R.id.title_name);
            this.userName = itemView.findViewById(R.id.userName);
            this.mContent = itemView.findViewById(R.id.content);
            this.mLogo = itemView.findViewById(R.id.logo);
            this.grade = itemView.findViewById(R.id.grade);
        }
    }


    public void showALert(Context context, String title, String content, String bt_name, DialogInterface.OnClickListener listener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(title);
        builder.setMessage(content);
        builder.setPositiveButton(bt_name, listener);
        builder.show();
    }
}
